package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.User;

import java.util.List;

@Dao
public interface UserDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long insertUser(User user);
    
    @Update
    int updateUser(User user);
    
    @Delete
    void deleteUser(User user);
    
    @Query("DELETE FROM users WHERE id = :id")
    void deleteUserById(int id);
    
    @Query("SELECT * FROM users WHERE id = :id")
    LiveData<User> getUserById(int id);
    
    @Query("SELECT * FROM users WHERE id = :id")
    User getUserByIdSync(int id);
    
    @Query("SELECT * FROM users WHERE username = :username")
    LiveData<User> getUserByUsername(String username);
    
    @Query("SELECT * FROM users WHERE username = :username")
    User getUserByUsernameSync(String username);
    
    @Query("SELECT * FROM users WHERE email = :email")
    LiveData<User> getUserByEmail(String email);
    
    @Query("SELECT * FROM users WHERE email = :email")
    User getUserByEmailSync(String email);
    
    @Query("SELECT * FROM users WHERE username = :usernameOrEmail OR email = :usernameOrEmail")
    User getUserByUsernameOrEmailSync(String usernameOrEmail);
    
    @Query("SELECT * FROM users ORDER BY id DESC")
    LiveData<List<User>> getAllUsers();
    
    @Query("SELECT * FROM users ORDER BY id DESC")
    List<User> getAllUsersSync();
    
    @Query("SELECT * FROM users WHERE role = 'ADMIN'")
    LiveData<List<User>> getAdminUsers();
    
    @Query("SELECT COUNT(*) FROM users WHERE username = :username")
    int countByUsername(String username);
    
    @Query("SELECT COUNT(*) FROM users WHERE email = :email")
    int countByEmail(String email);
    
    @Query("SELECT COUNT(*) FROM users WHERE username = :username OR email = :email")
    int countByUsernameOrEmail(String username, String email);
    
    @Query("UPDATE users SET password = :newPassword WHERE id = :userId")
    void updatePassword(int userId, String newPassword);
    
    @Query("UPDATE users SET email = :newEmail WHERE id = :userId")
    void updateEmail(int userId, String newEmail);
    
    @Query("UPDATE users SET nickname = :nickname WHERE id = :userId")
    void updateNickname(int userId, String nickname);
    
    @Query("UPDATE users SET role = :role WHERE id = :userId")
    void updateRole(int userId, String role);
} 